const chalk = require('chalk');
const path = require('path');
const fs = require('fs-extra');

function logSuccess (dir = '.', file = 'dart_model.dart') {
  return chalk.green('√', 'Dart Model 生成完毕,', `代码保存至: ${dir || '.'}/${file}`)
}

function output (data, dir = '.', file = 'dart_model.dart') {
  fs.outputFileSync(path.resolve(process.cwd(), dir, file), data);
}

function handle (context, opts = {}) {
  if (context === '') throw new Error('JSON数据错误，请检查后重试');

  if (opts.dir || opts.out) {
    output(context, opts.dir, opts.out);
    console.log(logSuccess(opts.dir, opts.out));
  } else {
    console.log(context);
  }
}

module.exports = handle;
